package offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class offer32 {
}

 //* Definition for a binary tree node.
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

class Solution32 {
    public static int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List list = new ArrayList();
        if(root==null)
        return null;
        queue.add(root);
        TreeNode node = root;
        while (!queue.isEmpty())
        {
            TreeNode temp = queue.poll();

            list.add(temp.val);
             if(temp.left!=null) {
                 queue.add(temp.left);
             }
             if(temp.right!=null) {
                 queue.add(temp.right);
             }
        }
        int [] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = (int) list.get(i);
        }
        return res;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        TreeNode node1 = new TreeNode(9);
        TreeNode node2 = new TreeNode(20);
        TreeNode node3 = new TreeNode(15);
        root.left = node1;
        root.right=  node2;
        node2.left = node3;
        int[] ints = levelOrder(root);

    }
}